import json
from  urllib import request,parse
from urllib.request import  Request
from urllib.parse import urlencode
# url = "http://httpbin.org/get?name=qiku"
#
# response = request.urlopen(url)
#
# if response.status== 200:
#
#     response_bytes = response.read()
#     print(response_bytes,11111)
#     response_text = response_bytes.decode("utf-8")
#     print(response_text,2222)
#     response_json = json.loads(response_text)
#     print(response_json,333)
#     print(response_json['headers']['User-Agent'],response_json['args'])


#
# url = "http://httpbin.org/get?name=qiku"
#
# #构建请求实例
# req = Request(url,headers={"user-agent":
#                                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0"},method='GET')
#
#
# response = request.urlopen(req)
#
# if response.status== 200:
#
#     response_bytes = response.read()
#     # print(response_bytes,11111)
#     response_text = response_bytes.decode("utf-8")
#     # print(response_text,2222)
#     response_json = json.loads(response_text)
#     # print(response_json,333)
#     print(response_json['headers']['User-Agent'],response_json['args'])




# url = "http://httpbin.org/post"
# data = {"age":"10","addr":"北京"}
# url_encode = urlencode(data)
# #构建请求实例
# req = Request(url,
#               data = url_encode.encode(),
#               headers={"user-agent":
#                                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0"},
#               #默认请求参数类型：application/x-www-form-urlencoded
#
#               method='POST'
#
#               )
#
# print(req,1111)
# response = request.urlopen(req)
#
# if response.status== 200:
#
#     response_bytes = response.read()
#     # print(response_bytes,11111)

#     response_text = response_bytes.decode("utf-8")
#     print(response_text,2222)
#     response_json = json.loads(response_text)
#     # print(response_json,333)
#     print(response_json['headers']['User-Agent'],response_json['args'])




#
# url = "http://httpbin.org/post"
# data = {"age":"10","addr":"北京"}
# url_encode = parse.urlencode(data).encode("utf-8")
# #构建请求实例
# req = Request(url,
#               data = url_encode,
#               headers={"user-agent":
#                                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0",
#
#                        },
#               method='POST',
#
#               )
#
#
# response = request.urlopen(req)
#
# if response.status== 200:
#
#     response_bytes = response.read()
#     # print(response_bytes,11111)
#     response_text = response_bytes.decode("utf-8")
#     print(response_text,2222)
#     response_json = json.loads(response_text)
#     # print(response_json,333)
#     print(response_json['headers']['User-Agent'],response_json['args'])


url = "http://httpbin.org/post?name=hbg"
data = {"age": "10", "addr": "北京"}
json_encode = json.dumps(data)
# 构建请求实例
req = Request(url,
              data=json_encode.encode(),
              headers={"user-agent":
                           "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0",
                       'Content-type': "application/json",
                       },
              # 默认请求参数类型：application/x-www-form-urlencoded

              method='POST'

              )

print(req, 1111)
response = request.urlopen(req)

if response.status == 200:
    response_bytes = response.read()
    # print(response_bytes,11111)
    response_text = response_bytes.decode("utf-8")
    print(response_text, 2222)
    response_json = json.loads(response_text)
    # print(response_json,333)
    print(response_json['headers']['User-Agent'], response_json['args'])
